//
//  CGAccountDefault.m
//  SchoolClient
//
//  Created by mac on 15/10/29.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGAccountDefault.h"

@implementation CGAccountDefault

+(instancetype)shareAccount{
    
    return [[self alloc]init];
}

/**
 *  实践证明，使用alloc方法初始化一个类的实例的时候，默认是调用了 allocWithZone 的方法。
 *  于是覆盖allocWithZone方法的原因已经很明显了：为了保持单例类实例的唯一性，需要覆盖所有会生成新的实例的方法，
 *  如果有人初始化这个单例类的时候不走[[Class alloc] init] ，而是直接 allocWithZone，
 *  那么这个单例就不再是单例了，所以必须把这个方法也堵上
 */
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    
    static CGAccountDefault *account;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        if (account == nil) {
            account = [super allocWithZone:zone];
            //从沙盒获取上次用户登录信息
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            account.version = [defaults objectForKey:@"version"];
            account.login = [defaults boolForKey:@"login"];
            account.isVip = [defaults boolForKey:@"vip"];
            account.isAutoLogin = [defaults boolForKey:@"autoLogin"];
            account.user_id = [defaults objectForKey:@"user_id"];
            account.account = [defaults objectForKey:@"account"];
            account.password = [defaults objectForKey:@"password"];
            account.tphone = [defaults objectForKey:@"tphone"];
            //account.headerIconData = [defaults objectForKey:@"headerIconData"];
            account.headerIconPath = [defaults objectForKey:@"headerIconPath"];
            account.name = [defaults objectForKey:@"name"];
            account.schoolName = [defaults objectForKey:@"schoolName"];
            account.studentId = [defaults objectForKey:@"studentId"];
            account.qq = [defaults objectForKey:@"qq"];
            account.idCard = [defaults objectForKey:@"idCard"];
            account.notices = [defaults objectForKey:@"notices"];
            
        }
    });
    
    return account;
}

//保存用户信息到沙盒中
-(void)saveToSandBox{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.version forKey:@"version"];
    [defaults setBool:self.login forKey:@"login"];
    [defaults setBool:self.isVip forKey:@"vip"];
    [defaults setBool:self.isAutoLogin forKey:@"autoLogin"];
    [defaults setObject:self.user_id forKey:@"user_id"];
    [defaults setObject:self.account forKey:@"account"];
    [defaults setObject:self.password forKey:@"password"];
    [defaults setObject:self.tphone forKey:@"tphone"];
    //[defaults setObject:self.headerIconData forKey:@"headerIconData"];
    [defaults setObject:self.headerIconPath forKey:@"headerIconPath"];
    [defaults setObject:self.name forKey:@"name"];
    [defaults setObject:self.schoolName forKey:@"schoolName"];
    [defaults setObject:self.studentId forKey:@"studentId"];
    [defaults setObject:self.qq forKey:@"qq"];
    [defaults setObject:self.idCard forKey:@"idCard"];
    [defaults setObject:self.notices forKey:@"notices"];
    
    //这里建议同步存储到磁盘中，这样一有信息就直接写进系统偏好设置的xml格式的.plist文件中
    [defaults synchronize];
    
}


@end
